﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class change_Detail_user : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        int point_distance, promotion_distance, sum_distance, user_id_add;
        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split(' ');
                stemp = stemp[0].Split('/');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                if (day2.Length == 1)
                {
                    day2 = "0" + day2;
                }
                if (mount2.Length == 1)
                {
                    mount2 = "0" + mount2;
                }
                return (years2 + "-" + mount2 + "-" + day2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "-" + mount3 + "-" + day3);
        }

        public DateTime ConvestToDate(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split('-');
                years = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                day = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                return new DateTime(years, mount, day);
            }
            catch (Exception)
            {

            }
            return new DateTime(years, mount, day);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 3)
                    {
                        int user_id = Int32.Parse(Session["user_id"].ToString());
                        string user_detail = @"select *
                                from Table_Passenger                                    
                                inner join District
                                    on Table_Passenger.Passenger_district = District.DistrictId
                                inner join Amphur
                                    on Table_Passenger.Passenger_sub_district = Amphur.AmphurId
                                inner join Province
                                    on Table_Passenger.Passenger_province = Province.ProvinceId
                                inner join Table_Category
	                                on Table_Passenger.Category_id = Table_Category.Category_id
                                where Passenger_id = " + user_id + "";
                        string distance = @"select Point_distance
                                from Table_Sale
                                inner join Table_Around
                                    on Table_Around.Around_id = Table_Sale.Around_id
                                inner join Table_Route
                                    on Table_Around.Route_id = Table_Route.Route_id
                                inner join Table_Passenger
                                    on Table_Sale.Passenger_id = Table_Passenger.Passenger_id
                                inner join Table_Point
                                    on Table_Sale.Point_id = Table_Point.Point_id 
                                where Table_Passenger.Passenger_id = " + user_id + " and Table_Sale.SSID = 1";
                        string promotion_item = @"select Table_promotion_item.promotion_item_distance
                                    from Table_Passenger
                                    inner join Table_store_user
                                    on Table_Passenger.Passenger_id = Table_store_user.Passenger_id
                                    inner join Table_promotion_item
                                    on Table_promotion_item.promotion_item_id = Table_store_user.promotion_item_id
                                    where Table_Passenger.Passenger_id = " + user_id + " and Table_promotion_item.promotion_item_status = 1";
                        SqlCommand sqlcomm = new SqlCommand();
                        string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                        SqlConnection sqlconn = new SqlConnection(Connection);
                        SqlDataAdapter da = new SqlDataAdapter(user_detail, sqlconn);
                        SqlDataAdapter da2 = new SqlDataAdapter(distance, sqlconn);
                        SqlDataAdapter da_P = new SqlDataAdapter(promotion_item, sqlconn);
                        DataTable dt = new DataTable();
                        DataTable dt2 = new DataTable();
                        DataTable dt_P = new DataTable();
                        da.Fill(dt);
                        da2.Fill(dt2);
                        da_P.Fill(dt_P);
                        try
                        {
                            TextBox18.Text = dt.Rows[0]["Passenger_id"].ToString();
                            Session["user_id_add"] = Int32.Parse(dt.Rows[0]["Passenger_id"].ToString());
                            DropDownList1.SelectedValue = dt.Rows[0]["Passenger_prefix"].ToString();
                            TextBox1.Text = dt.Rows[0]["Passenger_name"].ToString();
                            TextBox2.Text = dt.Rows[0]["Passenger_surname"].ToString();
                            DropDownList6.SelectedValue = dt.Rows[0]["Passenger_sex"].ToString();
                            TextBox4.Text = ConvestToDate_check(dt.Rows[0]["Passenger_birthday"].ToString());
                            TextBox5.Text = dt.Rows[0]["Passenger_home_no"].ToString();
                            TextBox6.Text = dt.Rows[0]["Passenger_village_no"].ToString();
                            TextBox7.Text = dt.Rows[0]["Passenger_lane"].ToString();
                            TextBox12.Text = dt.Rows[0]["Passenger_road"].ToString();
                            DropDownList2.SelectedValue = dt.Rows[0]["ProvinceId"].ToString().Trim();
                            DropDownList3.SelectedValue = dt.Rows[0]["AmphurId"].ToString().Trim();
                            DropDownList4.SelectedValue = dt.Rows[0]["DistrictId"].ToString().Trim();
                            TextBox8.Text = dt.Rows[0]["Passenger_post"].ToString();
                            TextBox9.Text = dt.Rows[0]["Passenger_mail"].ToString();
                            TextBox11.Text = dt.Rows[0]["Passenger_tel"].ToString();
                            TextBox3.Text = dt.Rows[0]["Passenger_identification"].ToString();
                            DropDownList7.SelectedValue = dt.Rows[0]["Category_id"].ToString();
                            if (dt2.Rows.Count <= 0)
                            {
                                point_distance = 0;
                            }
                            else
                            {
                                for (int i = 0; i < dt2.Rows.Count; i++)
                                {
                                    point_distance += Int32.Parse(dt2.Rows[i]["Point_distance"].ToString());
                                }
                            }
                            if (dt_P.Rows.Count <= 0)
                            {
                                promotion_distance = 0;
                            }
                            else
                            {
                                for (int i = 0; i < dt_P.Rows.Count; i++)
                                {
                                    promotion_distance += Int32.Parse(dt_P.Rows[i]["promotion_item_distance"].ToString());
                                }
                            }
                            sum_distance = point_distance - promotion_distance;
                            TextBox19.Text = point_distance.ToString();
                            TextBox20.Text = sum_distance.ToString();
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }                   
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            user_id_add = Int32.Parse(Session["user_id_add"].ToString());
            try
            {
                var q = from p in db.Table_Passengers
                        where p.Passenger_id == user_id_add
                        select p;
                q.ToList().ForEach(c => c.Passenger_prefix = DropDownList1.SelectedItem.ToString());
                q.ToList().ForEach(c => c.Passenger_name = TextBox1.Text);
                q.ToList().ForEach(c => c.Passenger_surname = TextBox2.Text);
                q.ToList().ForEach(c => c.Passenger_identification = TextBox3.Text);
                q.ToList().ForEach(c => c.Passenger_sex = DropDownList6.SelectedItem.ToString());
                q.ToList().ForEach(c => c.Passenger_birthday = ConvestToDate(TextBox4.Text));
                q.ToList().ForEach(c => c.Passenger_home_no = TextBox5.Text);
                q.ToList().ForEach(c => c.Passenger_village_no = TextBox6.Text);
                q.ToList().ForEach(c => c.Passenger_lane = TextBox12.Text);
                q.ToList().ForEach(c => c.Passenger_road = TextBox7.Text);
                q.ToList().ForEach(c => c.Passenger_district = Int32.Parse(DropDownList4.SelectedValue.ToString()));
                q.ToList().ForEach(c => c.Passenger_sub_district = Int32.Parse(DropDownList3.SelectedValue.ToString()));
                q.ToList().ForEach(c => c.Passenger_province = Int32.Parse(DropDownList2.SelectedValue.ToString()));
                q.ToList().ForEach(c => c.Passenger_post = TextBox8.Text);
                q.ToList().ForEach(c => c.Passenger_tel = TextBox11.Text);
                q.ToList().ForEach(c => c.Passenger_mail = TextBox9 .Text);
                q.ToList().ForEach(c => c.Category_id = Int32.Parse(DropDownList7.SelectedValue.ToString()));
                db.SubmitChanges();
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('แก้ไขข้อมูลสมาชิกแล้ว');", true);
                //Response.Write(@"<script language='javascript'>alert('แก้ไขข้อมูลสมาชิกแล้ว');</script>");
                Session["user_id"] = user_id_add;
                Response.Redirect("Detail_user_admin.aspx");
            }
            catch (Exception)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถแก้ไขข้อมูลสมาชิกได้');", true);
                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถแก้ไขข้อมูลสมาชิกได้');</script>");
                //throw;
            }         
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("Search_user.aspx");
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList3.Items.Clear();
            DropDownList4.Items.Clear();
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList4.Items.Clear();
        }
    }
}